
package de.fhhof.brst.session;

import java.io.IOException;
import java.io.Serializable;
import java.util.List;
import javax.faces.application.ConfigurableNavigationHandler;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ComponentSystemEvent;
import de.fhhof.brst.dao.EventDAO;
import de.fhhof.brst.dao.HibernateEventDAO;
import de.fhhof.brst.dao.HibernateUserDAO;
import de.fhhof.brst.dao.UserDAO;
import de.fhhof.brst.data.Booking;
import de.fhhof.brst.data.Event;
import de.fhhof.brst.data.User;
import de.fhhof.brst.exceptions.UserNotFoundException;
@ManagedBean
@SessionScoped
public class SessionBean implements Serializable {
  /**
   * 
   */
  private static final long serialVersionUID =  -4027802599187866387L;

  private boolean loggedIn =  false;

  private boolean adminStatus =  false;

  private de.fhhof.brst.data.User user;

  private de.fhhof.brst.data.Event selectedEvent;

  private de.fhhof.brst.dao.UserDAO userDAO =  HibernateUserDAO.getHibernateUserDAO();

  private boolean eventBooked =  false;

  public SessionBean() {
		user = new User();
		selectedEvent = new Event();
  }

  public String login() {
		String returnMessage = "";
		
		FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR,
				"Login unsuccesful!", "Please try again!");
		User tempUser = null;
		try {
			tempUser = userDAO.getUser(user.getEmail());
			if (tempUser.getPassword().equals(user.getPassword())) {
				message = new FacesMessage("Login succesful!");
				this.user = tempUser;
				loggedIn = true;
				if(tempUser.isAdmin()) this.adminStatus = true;
				returnMessage = "/user_profile_frame.xhtml?faces-redirect=true";
			}	
		} catch (UserNotFoundException e) {
			message = new FacesMessage(FacesMessage.SEVERITY_ERROR,
					"Login unsuccesful!", "User not found.");
		}

		FacesContext.getCurrentInstance().addMessage(null, message);
		
		return returnMessage;
  }

  public String logout() {
		this.loggedIn = false;
		this.adminStatus = false;
		this.user = new User();
		
		return "/default.xhtml?faces-redirect=true";
  }

  public void isAdmin(ComponentSystemEvent event) {

		FacesContext fc = FacesContext.getCurrentInstance();

		System.out.println(user.getEmail() + " " + user.isAdmin());
		if (!this.user.isAdmin()) {

			ConfigurableNavigationHandler nav = (ConfigurableNavigationHandler) fc
					.getApplication().getNavigationHandler();

			nav.performNavigation("access-denied_frame");
		}
  }

  public void isLoggedIn(ComponentSystemEvent event) {

		FacesContext fc = FacesContext.getCurrentInstance();

		if (!this.loggedIn) {

			ConfigurableNavigationHandler nav = (ConfigurableNavigationHandler) fc
					.getApplication().getNavigationHandler();

			nav.performNavigation("access-denied_frame");
		}
  }

  public de.fhhof.brst.data.User getUser() {
		return user;
  }

  public boolean getLoggedIn() {
		return this.loggedIn;
  }

  public void setLoggedIn(boolean loggedIn) {
		this.loggedIn = loggedIn;
  }

  public de.fhhof.brst.data.Event getSelectedEvent() {
		return selectedEvent;
  }

  public void setSelectedEvent(de.fhhof.brst.data.Event selectedEvent) {
		this.selectedEvent = selectedEvent;
		this.checkBookings();
  }

  public boolean isAdminStatus() {
		return adminStatus;
  }

  public void setAdminStatus(boolean adminStatus) {
		this.adminStatus = adminStatus;
  }

  public boolean isEventBooked() {
		return eventBooked;
  }

  public void setEventBooked(boolean eventBooked) {
		this.eventBooked = eventBooked;
  }

  private void checkBookings() {
		EventDAO eventDAO = HibernateEventDAO.getHibernateEventDAO();
		List<Booking> bookings = eventDAO.getBookings(user.getId());
		for (Booking booking : bookings) {
			if (booking.getEvent_id() == selectedEvent.getId()) {
				this.eventBooked = true;
				break;
			}
		}
  }

}
